﻿using AutoMapper;

using B.S.DataBase.MyProject.Domain.Sales;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Read.API.Applicantions.Command.SaleOrderInfoCommand;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.Myproject.Helper;
using B.S.DataBase.MyProject.Domain.ViewModels;
using B.S.DataBase.MyProject.Domain.Clients;

namespace T.E.ERP.Read.API.Applicantions.CommandHandler.SaleOrderInfoCommandHandler
{

    public class GetSaleOrderInfoListCommandHandler:IRequestHandler<GetSaleOrderInfoListCommand, APIResult<List<View_SaleOrderInfo>>>
    {
        private readonly IMapper mapper;
        private readonly IBaseRepository<CustomerProfiles> customerBiz;
        //private readonly IBaseRepository<Material_BasicModel> meterialModelBiz;
        private readonly IBaseRepository<SaleOrderInfo> orderBiz;
        private readonly IBaseRepository<OrderMaterials> orderMaterialsBiz;

        public GetSaleOrderInfoListCommandHandler(IMapper mapper, IBaseRepository<CustomerProfiles> customerBiz,IBaseRepository<SaleOrderInfo> orderBiz, IBaseRepository<OrderMaterials> orderMaterialsBiz)
        {
            this.mapper = mapper;
            this.customerBiz = customerBiz;
            //this.meterialModelBiz = meterialModelBiz;
            this.orderBiz = orderBiz;
            this.orderMaterialsBiz = orderMaterialsBiz;
        }

        public Task<APIResult<List<View_SaleOrderInfo>>> Handle(GetSaleOrderInfoListCommand request, CancellationToken cancellationToken)
        {
            var customers = customerBiz.GetAll().Result;
            //var materialModels = meterialModelBiz.GetAll().Result;
            var orderSales = orderBiz.GetAll().Result;
            var orderMaterials = orderMaterialsBiz.Quary(x => x.MaterialsClass == 1).Result;
            var list = from customersT in customers
                       join orderSalesT in orderSales
                       on customersT.Uid equals orderSalesT.CustomerId
                       join orderMaterialsT in orderMaterials
                       on orderSalesT.Id equals orderMaterialsT.SaleCode
                       //join materialModelsT in materialModels
                       //on orderMaterialsT.MaterialsCode equals materialModelsT.material_code
                       select new { customersT, orderSalesT, orderMaterialsT };
            if (request.Contract.IsNotNull())
            {
                list = list.Where(x => x.orderSalesT.Contract == request.Contract);
            }
            if (request.SaleUser.IsNotNull())
            {
                list = list.Where(x => x.orderSalesT.SaleUser == request.SaleUser);
            }
            if(request.SaleType != 0)
            {
                list = list.Where(x => x.orderSalesT.SaleType == request.SaleType);
            }
            if(request.CustomerName.IsNotNull())
            {
                list = list.Where(x => x.customersT.CustomerName == request.CustomerName);
            }
            if (request.MaterialsName.IsNotNull())
            {
                list = list.Where(x => x.orderMaterialsT.MaterialsName == request.MaterialsName);
            }
            if (request.Id.IsNotNull())
            {
                list = list.Where(x => x.orderSalesT.Id.ToString() == request.Id);
            }
            APIResult<List<View_SaleOrderInfo>> result = new APIResult<List<View_SaleOrderInfo>>();
            result.Count = list.Count();
            result.data = list.Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).OrderByDescending(a => a.orderSalesT.DocumentDate).Select(c =>
            new View_SaleOrderInfo
            {      
                   //单据编码
                   Id = c.orderSalesT.Id,
                   //单据状态
                   DocumentStatus = c.orderSalesT.DocumentStatus,
                   DocumentType = c.orderSalesT.DocumentType,
                   CustomerId = c.orderSalesT.CustomerId,
                   //单据日期
                   DocumentDate = c.orderSalesT.DocumentDate.DateTimeToString("yyyy-MM-dd HH:mm:ss","--"),
                   //客户名称
                   CustomerName = c.customersT.CustomerName,
                   //销售人员
                   SaleUser = c.orderSalesT.SaleUser,
                   //物料名称
                   MaterialsName = c.orderMaterialsT.MaterialsName,
                   //物料编码
                   MaterialsCode = c.orderMaterialsT.MaterialsCode,
                   //物料id
                   MaterialsId = c.orderMaterialsT.Id,
                   //数量
                  CalulateNumber = c.orderMaterialsT.CalulateNumber,
                  //单价
                  Price = c.orderMaterialsT.Price,
                  //金额
                  TotalPrice = c.orderMaterialsT.TotalPrice,
                  //发货数量
                  OutBoundNumber = c.orderMaterialsT.OutBoundNumber,
                  //销售类型[1 国内品牌  2 外购成品  3 外购原料  4  自接订单  5 自制销售  6 客供销售]
                  SaleType = c.orderSalesT.SaleType,
                
            }).ToList();
            result.Code = APIEnum.成功;
            result.Msg = APIEnum.成功.ToString();
            return Task.FromResult(result);
        }
    }
}
